package com.key.custom.plm.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.key.base.action.BaseAction;
import com.key.common.model.Page;
import com.key.custom.plm.model.Sap;
import com.key.custom.plm.service.ISapService;
import com.key.util.XspMessage;

public class SapAction extends BaseAction {

	ISapService sapService;
	private Sap sap;

	public Sap getSap() {
		return sap;
	}

	public void setSap(Sap sap) {
		this.sap = sap;
	}

	public ISapService getSapService() {
		if (sapService == null)
			sapService = (ISapService) getBean("sapServiceImp");
		return sapService;
	}

	public void setSapService(ISapService sapService) {
		this.sapService = sapService;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check

			getSapService().add(sap);
		} catch (Exception e) {
			return xspMessage
					.setMessage("保存失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check

			getSapService().update(sap);
		} catch (Exception e) {
			return xspMessage
					.setMessage("更新失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	private Page page;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Override
	public String list() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			page.setObjName(classname + " t");
			// 每页显示条数（默认10）
			page.setPageSize(15);
			page.setOrderBy(" PLM_TIME desc ");
			// 查询属性(为空查询所有)
			if (request.getParameter("partid") != null) {
				String partid = request.getParameter("partid");
				request.setAttribute("conditional", "?partid=" + partid);
				page.setCondition(" PRTNUM like '%" + partid + "%' ");
			}
			// 调用工厂获得数据
			page = getPagingFactory().querySapPart(page);
			request.setAttribute("page", page);
		} catch (Exception e) {
			return xspMessage.setMessage("查看列表失败", e.toString(),
					XspMessage.ERROR);
		}
		return "list";
	}

	@Override
	public String view() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			String id = request.getParameter("id");
			page.setObjName(classname + " t");
			page.setCondition(" id = '" + id + "' ");
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().querySapPart(page);
			String[] names = classname.split("[.]");
			String str = names[names.length - 1];
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
			if (page.getObjList() != null && page.getObjList().size() > 0) {
				request.setAttribute(str, page.getObjList().get(0));
			} else {
				request.setAttribute(str, null);
			}
		} catch (Exception e) {
			return xspMessage
					.setMessage("查看失败", e.toString(), XspMessage.ERROR);
		}
		return "view";
	}
}
